﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    public class DBRecommendation
    {


        //create
        // !NOTE -- recommendedID and recommendingID must be added
        // !NOTE -- this is because they do not have FK
        public static bool AddRecommendation(Recommendation r, Job j)
        {
            try
            {
                r.RecommendationID = "tempvalue";
                using (hireddbEntities db = new hireddbEntities())
                {
                    j.Recommendations.Add(r);
                    db.Recommendations.AddObject(r);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //retrieve recommendation by recommendationID
        public static Recommendation RetrieveRecommendation(string recommendationID)
        {
            Recommendation recommendation = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                recommendation =
                (from r in db.Recommendations
                 where r.RecommendationID == recommendationID
                 select r).SingleOrDefault();
            }
            return recommendation;
        }

        //retrieve list of job recommendations for an account
        public static List<Recommendation> RetrieveRecommendationListByRecommendedID(string recommendedID)
        {
            List<Recommendation> listOfRecommendations = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                listOfRecommendations =
                (from r in db.Recommendations
                 where r.RecommendedID == recommendedID
                 select r).ToList();
            }
            return listOfRecommendations;
        }

        //retrieve list of job recommendations an account has recommended
        public static List<Recommendation> RetrieveRecommendationListByRecommendingID(string recommendingID)
        {
            List<Recommendation> listOfRecommendations = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                listOfRecommendations =
                (from r in db.Recommendations
                 where r.RecommendingID == recommendingID
                 select r).ToList();
            }
            return listOfRecommendations;
        }

        //update recommendation
        public static bool UpdateRecommendation(Recommendation r)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Recommendation recommendationToUpdate = RetrieveRecommendation(r.RecommendationID);
                    db.Attach(recommendationToUpdate);
                    recommendationToUpdate.Contents = r.Contents;
                    recommendationToUpdate.Timestamp = r.Timestamp;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //delete recommendation
        public static bool DeleteRecommendation(Recommendation r)
        {
            try
            {
                Recommendation recommendationToDelete = RetrieveRecommendation(r.RecommendationID);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(recommendationToDelete);
                    db.DeleteObject(recommendationToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //check if the same job has been recommended by the same recommender account to the same recommended account
        public static bool CheckRecommendation(string jobID, string recommendingID, string recommendedID)
        {
            try
            {
                Recommendation recomm = null;
                using (hireddbEntities db = new hireddbEntities())
                {
                    recomm =
                    (from r in db.Recommendations
                     where r.JobID == jobID && r.RecommendingID == recommendingID && r.RecommendedID == recommendedID
                     select r).SingleOrDefault();
                }

                if (recomm != null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        //rank job according to number of recommendations of the same job made to the same account
        public static List<object> RankJobRecommendation(string recommendedID)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                (from r in db.Recommendations
                 where r.RecommendedID == recommendedID
                 group r by r.JobID into j
                 select new { JobID = j.Key, RecommCount = j.Count() }).ToList<object>();
            }
        }
    }
}